package com.doctor.ui.medicalknowledge;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.doctor.adapter.KnowledgeFirstAdapter;
import com.doctor.bean.KnowledgeBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.FragmentHelper;
import com.doctor.comm.LoadingTip;
import com.doctor.comm.MyApplication;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;

public class KnowledgeFirstFrag extends BaseFragment{
	
	/**
	 * 医学知识医疗技术一级目录
	 */
	
	private Context context;
	private ListView listView;
	private KnowledgeFirstAdapter adapter;
	private List<KnowledgeBean> beanList=new ArrayList<KnowledgeBean>();
	public static final int parentId=0; 
	private KnowledgeSecondFrag secondFrag;
	private KnowledgeFourthFrag fourthFrag;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context = getActivity();
		
		View view=inflater.inflate(R.layout.knowledge_first, container, false);
		initiaView(view);
		
		return view;
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		context=null;
		listView=null;
		adapter=null;
		beanList=null;
		secondFrag=null;
	}
	
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		listView=(ListView) view.findViewById(R.id.listView);
		beanList=new ArrayList<KnowledgeBean>();
		adapter=new KnowledgeFirstAdapter(context,beanList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				KnowledgeBean bean=(KnowledgeBean) arg1.getTag(R.id.bean_tag);
				Bundle bundle=new Bundle();
				bundle.putSerializable(ConstConfig.BEAN, bean);
				FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
				FragmentHelper.setFragSwitchAnimation(transaction);
				
				//新栏目点击进去之后就不再是新的栏目了
				boolean isNew = bean.getIsNew();
				if (isNew) {
					ContentValues values = new ContentValues();
					values.put("_is_new", 0);
					int result = DbOperator.getInstance(MyApplication.getInstance()).updateData(ConstConfig.CATEGORT_TABLE, values, bean.getId());
					if (result > 0) {
						//把非新栏目的标志值存进数据库里保存起来
						bean.setIsNew(false);
						adapter.notifyDataSetChanged();
					}
				}
				
				if (bean.getId()==50) {
					//家庭医生健康管理只有两级目录，这里进行特别处理，调用四五级菜单界面
					fourthFrag=null;
					fourthFrag=new KnowledgeFourthFrag();
					fourthFrag.setArguments(bundle);
					transaction.replace(R.id.frag_container, fourthFrag).addToBackStack(null).commit();
					return;
				} else if (bean.getId()==51) {
					//医学视频也只有两级目录，调用四五级菜单
					fourthFrag=null;
					fourthFrag=new KnowledgeFourthFrag();
					fourthFrag.setArguments(bundle);
					transaction.replace(R.id.frag_container, fourthFrag).addToBackStack(null).commit();
					return;
				}
				
				secondFrag=null;
				secondFrag=new KnowledgeSecondFrag();
				secondFrag.setArguments(bundle);
				transaction.replace(R.id.frag_container, secondFrag).addToBackStack(null).commit();
			}
		});
		
		LoadingTip.showDialog(context, getString(R.string.loading));
		new Thread() {
			public void run() {
				//一级栏目数据
				final List<KnowledgeBean> list = DbOperator.getInstance(context).selectKnowledgeData(parentId);
				getActivity().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						if (list != null && list.size() > 0) {
							adapter.addAll(list);
						}
						LoadingTip.hideDialog();
					}
				});
			}
		}.start();
	}
	
	
}
